include tools/makedefaults.inc

INCLUDES = -Isrc/inc
OS_PRINTFOP = -Wl,-u,vfprintf -lprintf_flt -lm
#MEMLAYOUT = -Wl,--defsym=__malloc_heap_end=0x800800
OS_LDFLAGS = $(OS_PRINTFOP) $(MEMLAYOUT)

OSNAME = bin/libjoyos
OSHEX = $(OSNAME).hex
OSLIB = $(OSNAME).a
OSELF = $(OSNAME).elf

BOOTHEX = bin/hboot.hex

# HAL source files
HALSRC = 	io adc spi uart delay

# Kernel source files
KERNELSRC = main lock board fork panic thread isr util

# Driver source files
DRIVERSRC = lcd servo motor analog \
			digital encoder buttons gyro \
			devices/fpga \
			devices/mcp3008 \
			devices/nrf24l01 \
			devices/at45db011 \

# Source files for os library
SRC = 	$(DRIVERSRC:%=src/drivers/%.c) \
  		$(KERNELSRC:%=src/kern/%.c) \
		$(HALSRC:%=src/hal/%.c)

# ALL source files
USERSRC = $(SRC) user/umain.c

# Objects for library
OBJ = $(SRC:.c=.o)

# OS object files
USEROBJ = $(USERSRC:.c=.o)

# ---- Targets ----
# All targets
all: bootloader happylib joyos size

# ---- Programming Targets ----
# program happytest
program: $(OSHEX)
	$(AVRDUDE) $(AVRDUDEFLAGS_USER) -U flash:w:$(OSHEX)

# program bootloader
programboot: bootloader
	$(AVRDUDE) $(AVRDUDEFLAGS_JTAG) -U flash:w:$(BOOTHEX)

# program fuses
programfuses:
	$(AVRDUDE) $(AVRDUDEFLAGS_JTAG) -V -U lfuse:w:0xef:m -U hfuse:w:0x98:m -U efuse:w:0xff:m

# program FTDI
programftdi:
	$(FTDI_EEPROM) --flash-eeprom tools/ftdi.conf

# avrdude terminal
programterm:
	$(AVRDUDE) $(AVRDUDEFLAGS_JTAG) -t

# ---- Main Targets ----
# Bootloader
bootloader:
	@echo "Building Happyboot"
	@make -C src/boot

# Happylib
happylib:
	@echo "Building Happylib"
	@make -C src/lib

# Joyos
joyos-start:
	@echo "Building JoyOS"

joyos: joyos-start $(OSLIB) $(OSHEX)

# ---- Basic compilation targets ----
%.o: %.c
	@echo " - Compiling $@"
	@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

%.hex: %.elf
	@echo " - Generating hex file $@"
	@$(OBJCOPY) -S -O ihex -R .eeprom $< $@

$(OSELF): $(USEROBJ)
	@echo " - Linking $@"
	@$(CC) $(CFLAGS) $(USEROBJ) -o $@ $(OS_LDFLAGS)

$(OSLIB): $(OBJ)
	@echo " - Archiving" $@
	@$(AR) rcs $@ $(OBJ)

# ---- Misc Support Targets ----
# Code Size
size: $(OSELF)
	@/bin/echo -n " - OS Size "
	@tools/avr-mem.sh $(OSELF) $(MCU)

clean:
	@echo " - Cleaning objects"
	@rm -f $(OBJ)
	@rm -f $(OSELF) $(OSHEX) $(OSLIB)
	@make -C src/boot clean
	@make -C src/lib clean
	@rm -f gdbinit
	@rm -rf release/*
	@rm -rf doc/api/*

gdb-config:
	@rm -f gdbinit
	@echo "define reset" >> gdbinit
	@echo "SIGNAL SIGHUP" >> gdbinit
	@echo "end" >> gdbinit
	@echo "file $(OSELF)" >> gdbinit
	@echo "target remote localhost:4242" >> gdbinit
	@echo "break main" >> gdbinit

debug: $(OSELF) gdb-config
	@echo " - Starting debugger"
	@avarice -D -e -p --file $(OSELF) --jtag $(AVRDUDE_PORT) localhost:4242
	@avr-gdb --command=gdbinit

docs:
	@echo " - Generating documentation"
	@( cat doc/doxygen/Doxyfile ; ./tools/get_version.sh ) | doxygen -
	@cp doc/doxygen/reset.css doc/api
	@cp doc/doxygen/main.css doc/api
	@rm doc/api/tab* doc/api/doxygen.png

release: $(OSLIB) happylib size docs
	@echo " - Making release"
	@mkdir -p release
	@rm -rf release/*
	@mkdir release/6.270/
	@mkdir release/6.270/lib
	@mkdir release/6.270/src
	@cp -R src/inc release/6.270/inc
	@cp -R doc/api release/6.270/doc
	@cp bin/*.a release/6.270/lib
	@cp -R user/irdistcal release/6.270/src
	@cp -R user/irdisttest release/6.270/src
	@cp -R user/happytest release/6.270/src
	@cp -R user/gyrotest release/6.270/src
	#@cp -R user/motiontest release/6.270/src
	@cp -R user/robot release/6.270/src
	@cp -R user/makedefaults.inc release/6.270/src
	@find release/ -name ".svn" | xargs rm -rf

dist: release
	@./tools/make_release.sh

distclean:
	@rm -rf dist/*
.PHONY: release
